<?php 

class MemberAction extends Action{
    // 首页 
    public function index(){
		$this->redirect('checkLogin');
    } 
	// 登录检测
    public function checkLogin(){
		$userEmail		=	isset($_POST["username"]) ? $_POST["username"] : Cookie::get("username");
		$userPassword	=	isset($_POST["password"]) ? md5($_POST["password"]) : Cookie::get("password");
		
		//验证用户信息
		$Member	=	D('Member');
		$user = D("Member")->count("username='$username' and password='$userPassword'");
		//$user		=	$Member->find("username='$username' and password='$userPassword'",'uid,username,email,password');

		if($user){

			//更新登录时间
			$Member->find('uid='.$user->uid);
			$Member->lastlogintime = time();
			$Member->logintimes = $Member->logintimes+1;
			$Member->save();
			//记录登陆状态
			Session::set('uid',$user->uid);
			Session::set('userInfo',$user);
			Cookie::set('email',$userEmail,36000000);
			//记住登录
			if($_POST["autologin"] == 'on'){
				Cookie::set("password",$userPassword,36000000);
			}
			unset($userEmail,$userPassword);
			//跳转到Home页
			$this->redirect('index','Home');
		//验证失败
		}else{
			//跳转到登陆页面
			$this->redirect("login");

		}
    } 
	// 登录
	public function login(){
		$this->display();
	}
    // 注册 
    public function reg(){
		$this->display();
    } 
    // 检查用户名 
	public function checkUsername() {
		$num = D("Member")->count('username ="'.trim($_POST['username']).'"');
		if ( $num >0 ) {
			echo false;
		}else{
			echo true;
		}
	}
    // 检查邮箱 
	public function checkEmail() {
		$num = D("Member")->count('email ="'.trim($_POST['email']).'"');
		if ( $num >0 ) {
			echo false;
		}else{
			echo true;
		}
	}
    // 检查验证码 
	public function checkVerify() {
		if(md5($_POST['verify']) != Session::get('verify')) {
			echo false;
		}else{
			echo true;
		}
	}
    // 处理表单数据 
    public function doreg() {
		if(!empty($_POST['username']) && !empty($_POST['email'])){
			$Member = D("Member"); 
			if($user = $Member->create()){
				$Member->password	= md5($_POST["password"]);
				$ip = $Member->getIP();
				$Member->regip = $ip;
				$Member->lastloginip = $ip;
				if($newuser = $Member->add()){
					$this->uid	=	$newuser;
					$userInfo['username']	=	$user->username;
					$userInfo['email']	=	$user->email;
					$userInfo['password']	=	$user->password;
					Session::set('uid',$newuser);
					Session::set('userInfo',$userInfo);
					$this->redirect("index");
				}else{
					$this->error("注册失败！");
				}
				
			}
		}else{
			$this->redirect("reg");
		}
    } 
    // 生成验证码 
    public function verify() { 
        import("ORG.Util.Image"); 
		Image::buildImageVerify();  
    } 
} 
?>